#include "../Include/xboxcontroller.h"

XBOXController::XBOXController(int id)
{
	this->id = id;
}

bool XBOXController::UpdateState()
{
	ZeroMemory(&this->state, sizeof(XINPUT_STATE));
	DWORD controllerState = XInputGetState(this->id, &this->state);

	return (controllerState == ERROR_SUCCESS);
}

WORD XBOXController::GetButtons()
{
	return this->state.Gamepad.wButtons;
}

unsigned char XBOXController::GetTrigger(int side)
{
	if(side == LEFT) {
		return this->state.Gamepad.bLeftTrigger;
	} else if(side == RIGHT) {
		return this->state.Gamepad.bRightTrigger;
	}

	return 0;
}

Vector2i XBOXController::GetStick(int side)
{
	if(side == LEFT) {
		Vector2i left;
		left.x = this->state.Gamepad.sThumbLX;
		left.y = this->state.Gamepad.sThumbLY;
		return left;
	} else if(side == RIGHT) {
		Vector2i right;
		right.x = this->state.Gamepad.sThumbRX;
		right.y = this->state.Gamepad.sThumbRY;
		return right;
	}

	Vector2i null;
	null.x = 0;
	null.y = 0;
	return null;
}

void XBOXController::SetVibration(unsigned short left, unsigned short right)
{
	XINPUT_VIBRATION vibration;
	vibration.wLeftMotorSpeed = left;
	vibration.wRightMotorSpeed = right;
	XInputSetState(this->id, &vibration);
}